package com.czwj.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.ComboxNode;
import com.czwj.entities.DataGridData;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.Authority;
import com.czwj.entities.Level;
import com.czwj.entities.UserInfo;
import com.czwj.service.AuthorityService;
import com.czwj.service.LevelService;
import com.czwj.service.UserInfoService;
import com.czwj.util.TextUtil;

/**
* @ClassName: LevelAction
* @Description: TODO
* @author H
* @date 2020年4月10日
*
*/
@Controller
public class LevelAction{
	@Resource
	AuthorityService authorityService;
	@Resource
	LevelService levelService;
	@Resource
	UserInfoService userInfoService;
	
	/**
	* @Title: getLevels
	* @Description: 角色查询列表
	* @param page
	* @param rows
	* @return 参数 
	* @return DataGridData<Level> 返回类型
	* @throws
	*/
	@RequestMapping(value = "/levels",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Level> getLevels(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows){
		DataGridData<Level> result = new DataGridData<>();
		Map<String,Object> params = new HashMap<>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows))
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		if(pageBean != null){
			params.put("start",pageBean.getStart());
			params.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		params.put("companyid",userInfo.getCompanyid());
		List<Level> levels = levelService.search(params);
		int total = levelService.getTotal(params);
		result.setRows(levels);
		result.setTotal(total);
		return result;
	}
	
	@RequestMapping(value = "/levellist",method = RequestMethod.POST)
	@ResponseBody
	public List<Level> getAllLevels(@RequestParam(value = "companyid",required = false) Integer companyid){
		Map<String,Object> map = new HashMap<>();
		map.put("companyid",companyid);
		List<Level> list = levelService.search(map);
		return list;
	}
	
	@RequestMapping(value = "/levellist2",method = RequestMethod.POST)
	@ResponseBody
	public List<Level> getAllLevels2(HttpServletRequest request,HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");
		List<Level> list = levelService.getLevelList2();
		return list;
	}
	
	@RequestMapping(value = "level",method = RequestMethod.GET)
	@ResponseBody
	public Level getLevelById(@RequestParam("levelId") int levelId){
		return levelService.selectByPrimaryKey(levelId);
	}
	
	/**
	* @Title: getComboxLevels
	* @Description: TODO
	* @return 参数 
	* @return List<ComboxNode> 返回类型
	* @throws
	*/
	@RequestMapping(value = "combox/levels",method = RequestMethod.GET)
	@ResponseBody
	public List<ComboxNode> getComboxLevels(){
		List<ComboxNode> nodes = new ArrayList<>();
		Map<String,Object> params = new HashMap<>();
		List<Level> levels = levelService.search(params);
		for(Level level:levels){
			ComboxNode node = new ComboxNode(level.getId(),level.getName(),false,level.getName());
			nodes.add(node);
		}
		ComboxNode firstNode = new ComboxNode(0,"请选择",true,"请选择");
		nodes.add(0,firstNode);
		return nodes;
	}
	
	/**
	* @Title: levelSave
	* @Description: 更新角色
	* @param level
	* @return 参数 
	* @return OperateResult 返回类型
	* @throws
	*/
	@SystemControllerLog(description = "更新角色")
	@RequestMapping(value = "/levelSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult levelSave(@RequestBody Level level){
		OperateResult result = new OperateResult();
		int count = 0;
		if(level.getId() != null && level.getId() > 0)
			count = levelService.updateByPrimaryKeySelective(level);
		else{
			Subject currentUser = SecurityUtils.getSubject();
			UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
			level.setCompanyid(userInfo.getCompanyid());
			count = levelService.insert(level);
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	/**
	* @Title: setAuthoritys
	* @Description: 更新权限
	* @param params
	* @return 参数 
	* @return OperateResult 返回类型
	* @throws
	*/
	@SystemControllerLog(description = "更新权限")
	@RequestMapping(value = "/authority/level.do",method = RequestMethod.PUT)
	@ResponseBody
	public OperateResult setAuthoritys(@RequestBody Map<String,Object> params){
		OperateResult result = new OperateResult();
		int levelid = Integer.valueOf(params.get("levelId").toString());
		int projectid = Integer.valueOf(params.get("perId").toString());
		boolean check = Boolean.valueOf(params.get("check").toString());
		Map<String,Object> rpParams = new HashMap<>();
		rpParams.put("levelid",levelid);
		rpParams.put("projectid",projectid);
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		Integer companyid = userInfo.getCompanyid();
		rpParams.put("companyid",companyid);
		List<Authority> rps = authorityService.search(rpParams);
		int count = 0;
		if(check){
			if(rps.size() <= 0){
				Authority levelPer = new Authority();
				levelPer.setProjectid(projectid);
				levelPer.setLevelid(levelid);
				levelPer.setCompanyid(companyid);
				count = authorityService.add(levelPer);
			}
		}else{
			if(rps.size() > 0){
				for(Authority rp:rps){
					count += authorityService.delete(rp.getId());
				}
			}
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	/**
	* @Title: deleteLevel
	* @Description: 删除角色
	* @param levelId
	* @return 参数 
	* @return OperateResult 返回类型
	* @throws
	*/
	@SystemControllerLog(description = "删除角色")
	@RequestMapping(value = "/levelDelete.do",method = RequestMethod.DELETE)
	@ResponseBody
	public OperateResult deleteLevel(@RequestParam("levelId") int levelId){
		OperateResult result = new OperateResult();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("levelid",levelId);
		List<UserInfo> list = userInfoService.search(map);
		if(list != null){
			for(UserInfo userInfo:list){
				userInfo.setLevelid(4);
				int num = userInfoService.update(userInfo);
			}
		}
		authorityService.deleteByLevelId(levelId);
		int count = levelService.delete(levelId);
		result.setSuccess(count > 0);
		return result;
	}
}
